import axios,{ type AxiosResponse , type AxiosRequestHeaders } from 'axios'
import { showSuccessToast, showFailToast } from 'vant';
interface RequestModel<T> {
  code:string | number
  data:T
  message?:string
}

const wyrequest = axios.create({
  baseURL: "/wy",
  timeout:5000
})

wyrequest.interceptors.request.use(
  (config) => {
    return config
  }
)

wyrequest.interceptors.response.use(
  async (response: AxiosResponse<RequestModel<any> >) => {
    const res = response.data;
    
    // if(res.code !== "20000" && res.code !== "200"){
    //   showFailToast((typeof res.data=='string' && res.data)|| res.message || 'Error');
    // }else {
    //   return res.data
    // }
    return res.data
  },
  (error) => {
		// 对响应错误做点什么
		if (error.message.indexOf('timeout') != -1) {
			showFailToast('网络超时');
		} else if (error.message == 'Network Error') {
			showFailToast('网络连接错误');
		} else {
			if (error.response.data) showFailToast(error.response.statusText);
			else showFailToast('接口路径找不到');
		}
		return Promise.reject(error);
	}
)


export default wyrequest